package com.yjr.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.yjr.gulimall.product.entity.AttrEntity;
import com.yjr.gulimall.product.service.AttrAttrgroupRelationService;
import com.yjr.gulimall.product.service.AttrService;
import com.yjr.gulimall.product.service.CategoryService;
import com.yjr.gulimall.product.vo.AttrGroupRelationVO;
import com.yjr.gulimall.product.vo.AttrGroupWithAttrsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yjr.gulimall.product.entity.AttrGroupEntity;
import com.yjr.gulimall.product.service.AttrGroupService;
import com.yjr.common.utils.PageUtils;
import com.yjr.common.utils.R;



/**
 * 属性分组
 *
 * @author yjr
 * @email 2608509744@qq.com
 * @date 2021-08-29 13:15:49
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catelogId")Long catelogId){
        //PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        Long[] cateLogPaths=categoryService.findCateLogPath(catelogId);
        attrGroup.setCatelogPath(cateLogPaths);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    @RequestMapping("/attr/relation/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVO[] attrGroupRelationVOS){

        attrAttrgroupRelationService.deleteRelation(attrGroupRelationVOS);

        return R.ok();
    }

    @GetMapping("/{attrgroupId}/attr/relation")
    //@RequiresPermissions("product:attr:delete")
    public R getAttrGroupRelationList(@PathVariable("attrgroupId")Long attrgroupId){

        List<AttrEntity> attrEntities=attrService.getAttrRelationList(attrgroupId);

        return R.ok().put("data",attrEntities);
    }


    @GetMapping("/{attrgroupId}/noattr/relation")
    //@RequiresPermissions("product:attr:delete")
    public R getAttrGroupRelationList(@RequestParam Map<String, Object> params,@PathVariable("attrgroupId")Long attrgroupId){



        PageUtils page=attrService.getAttrGroupRelationList(attrgroupId,params);


        return R.ok().put("page", page);
    }

    // /product/attrgroup/{catelogId}/withattr
    @GetMapping("/{catelogId}/withattr")
    public R getAttrgroupWithAttrs(@PathVariable("catelogId") Long catelogId) {
        List<AttrGroupWithAttrsVO> attrGroupWithAttrsVOS=attrGroupService.getAttrgroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data",attrGroupWithAttrsVOS);

    }

    @PostMapping("/attr/relation")
    public R attrRelation(@RequestBody List<AttrGroupRelationVO> attrGroupRelationVOs){

        attrAttrgroupRelationService.saveRelation(attrGroupRelationVOs);
        return  R.ok();
    }
}
